package com.vessel.decoration.mvp.ui.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.vessel.decoration.BuildConfig;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportActivity;
import com.vessel.decoration.app.data.entity.ProductListResponse;
import com.vessel.decoration.di.component.mine.DaggerApplyComponent;
import com.vessel.decoration.di.module.mine.ApplyModule;
import com.vessel.decoration.mvp.contract.mine.ApplyContract;
import com.vessel.decoration.mvp.presenter.mine.ApplyPresenter;

import butterknife.BindView;
import butterknife.OnClick;

import static com.vessel.decoration.app.constant.Constants.TYPE;
import static com.vessel.decoration.app.constant.Constants.TYPE_APPLY_BUSINESS;
import static com.vessel.decoration.app.constant.Constants.TYPE_APPLY_WORKER;

/**
 * @author vesselzhang
 * @date 2017/12/8
 */
@Route(path = "/app/apply")
public class MeApplyActivity extends MySupportActivity<ApplyPresenter> implements ApplyContract.View {

    @BindView(R.id.tv_title)
    TextView mTitleTV;
    @BindView(R.id.apply_real_name)
    EditText mNameEV;
    @BindView(R.id.apply_number)
    EditText mNumberEV;
    @BindView(R.id.apply_phone)
    EditText mPhoneEV;
    @BindView(R.id.apply_type)
    TextView mTypeTV;
    @BindView(R.id.apply_card_front)
    ImageView mImageFrontIV;
    @BindView(R.id.apply_card_back)
    ImageView mImageBackIV;

    private int type;
    private ProductListResponse.ProductsBean product;
    private AppComponent mAppComponent;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerApplyComponent
                .builder()
                .appComponent(appComponent)
                .applyModule(new ApplyModule(this))
                .build()
                .inject(this);
        this.mAppComponent = appComponent;
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_me_userinfo_apply;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        type = getIntent().getIntExtra(TYPE, TYPE_APPLY_BUSINESS);
        switch (type) {
            case TYPE_APPLY_BUSINESS:
                mTitleTV.setText("商家权限申请");
                break;
            case TYPE_APPLY_WORKER:
                mTitleTV.setText("技工权限申请");
                break;
        }
        mPresenter.queryTypeList(type);
    }

    @OnClick({R.id.apply_card_front_add, R.id.apply_card_back_add, R.id.apply_type, R.id.apply_submit, R.id.iv_left})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.apply_card_front_add:
                mPresenter.addImage(true);
                break;
            case R.id.apply_card_back_add:
                mPresenter.addImage(false);
                break;
            case R.id.apply_type:
                mPresenter.showTypePicker();
                break;
            case R.id.apply_submit:
                mPresenter.submit(type, mNameEV.getText().toString()
                        , mPhoneEV.getText().toString(), mNumberEV.getText().toString());
                break;
            case R.id.iv_left:
                killMyself();
                break;
        }
    }

    @Override
    public void showFrontPic(String uri) {
        mImageFrontIV.setVisibility(View.VISIBLE);
        mAppComponent.imageLoader()
                .loadImage(activity, ImageConfigImpl
                        .builder()
                        .imageView(mImageFrontIV)
                        .url(BuildConfig.APP_DOMAIN + uri)
                        .build());
    }

    @Override
    public void showBackPic(String uri) {
        mImageBackIV.setVisibility(View.VISIBLE);
        mAppComponent.imageLoader()
                .loadImage(activity, ImageConfigImpl
                        .builder()
                        .imageView(mImageBackIV)
                        .url(BuildConfig.APP_DOMAIN + uri)
                        .build());
    }

    @Override
    public void showType(String type) {
        mTypeTV.setText(type);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {
        ArmsUtils.makeText(activity, message);
    }

    @Override
    public void launchActivity(Intent intent) {

    }

    @Override
    public void killMyself() {
        finish();
    }
}
